package edu.zzuli.utils;

import java.sql.*;

/**
 * Created by Chen-D.W on 2017/12/25.
 */
public class DbUtil {

    private static final String url="jdbc:mysql://127.0.0.1:3306/mail";


    private static final String username="root";

    private static final String password="123456";

    private static final String driver="com.mysql.jdbc.Driver";

    private static Connection connection=null;


    static {
        try {
            Class.forName(driver);
            connection= DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public  Connection getConnection() {
        if(connection==null){
            try {
                connection=DriverManager.getConnection(url,username,password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }

    public void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
        try {
            if (resultSet!=null){
                resultSet.close();
            }
            if(preparedStatement!=null){
                preparedStatement.close();
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
